//
//  ByViewPhysicalCheckBuy.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/12/12.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewPhysicalCheckBuy.h"

// model
#import "ByModelPhysicalBuyCate.h"
#import "ByModelPhysicalFixed.h"

// other
#import "ZKSegmentControl.h"

// view
#import "ByViewPhysicalBuyHeader.h"
#import "ByViewPhysicalBuySegment.h"
#import "ByViewOptionalClassScroll.h"
#import "ByViewOptionalSubClassScroll.h"

// cell
#import "ByCellPhysicalFixed.h"

@interface ByViewPhysicalCheckBuy()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *cates;
@property (nonatomic, strong) ByViewPhysicalBuyHeader *headerView;
@property (nonatomic, strong) UIView *bgView;

// 用于筛选的数据
@property (nonatomic, copy) NSString *cate_id;
@property (nonatomic, assign) NSInteger select_index;

@property (nonatomic, strong) UIView *contentView;

// 固定套餐
@property (nonatomic, strong) NSMutableArray *fixedAry;
@property (nonatomic, strong) UITableView *fixedTable;

// 自选
@property (nonatomic, strong) NSMutableArray *optionalAry;
@property (nonatomic, strong) ByViewOptionalClassScroll *classScroll;
@property (nonatomic, strong) ByViewOptionalSubClassScroll *subClassScroll;
@end

@implementation ByViewPhysicalCheckBuy
- (void)setupUI{
    self.backgroundColor = BY_TABLE_COLOR;
    __weak typeof(self) weakSelf = self;
    self.contentViewDidAppear = ^(NSInteger index) {
        [weakSelf fetchData];
        [weakSelf fetchDetailData];
    };
}
#pragma mark - getter
- (NSMutableArray *)cates{
    if (!_cates) {
        _cates = [NSMutableArray array];
    }
    return _cates;
}

- (NSMutableArray *)fixedAry{
    if (!_fixedAry) {
        _fixedAry = [NSMutableArray array];
    }
    return _fixedAry;
}

- (NSMutableArray *)optionalAry{
    if (!_optionalAry) {
        _optionalAry = [NSMutableArray array];
    }
    return _optionalAry;
}
#pragma mark - 获取数据
// 获取全部数据
- (void)fetchData{
    if (self.cates.count != 0) {
        [self.cates removeAllObjects];
    }
    // 分类
    for (int i = 0; i < 10; i ++) {
        ByModelPhysicalBuyCate *model = [[ByModelPhysicalBuyCate alloc] init];
        model.name = [NSString stringWithFormat:@"分类%d",i];
        [self.cates addObject:model];
    }
    ByModelPhysicalBuyCate *all_model = [[ByModelPhysicalBuyCate alloc] init];
    all_model.name = @"全部";
    [self.cates insertObject:all_model atIndex:0];
    
    [self createViews];
}

// 筛选数据
- (void)fetchDetailData{
    if (self.select_index == 0) {
        for (int i = 0; i < 10; i ++) {
            ByModelPhysicalFixed *model = [[ByModelPhysicalFixed alloc] init];
            model.title = @"主标题主标题";
            model.subtitle = @"副标题";
            model.price = @"324";
            model.originPrice = @"237";
            [self.fixedAry addObject:model];
            [self createFixedViews];
        }
    }else{
        // 测试数据
        for (int i = 0; i < 10; i ++) {
            ByModelPhysicalOptional *model = [[ByModelPhysicalOptional alloc] init];
            model.name = [NSString stringWithFormat:@"分类%d",i];
            ByModelPhysicalOptional *s_model = [[ByModelPhysicalOptional alloc] init];
            s_model.name = [NSString stringWithFormat:@"子分类%d测试测试测试测试测试",i];
            s_model.title = @"甲状腺";
            s_model.price = @"243.00";
            model.children = @[s_model,s_model];
            [self.optionalAry addObject:model];
        }
       
        [self createOptionalViews];
    }
}

#pragma mark - create views
- (void)createViews{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    
    // 1.主视图
    UIView *mainView = [[UIView alloc] init];
    [self addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(50));
        make.right.equalTo(@(0));
        make.bottom.equalTo(@(0));
    }];
    mainView.backgroundColor = [UIColor whiteColor];
    
    // 1.创建筛选的分类
    self.headerView = [[ByViewPhysicalBuyHeader alloc] init];
    [self addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    self.headerView.items = self.cates;
    
    // 显示bgView
    __weak typeof(self) weakSelf = self;
    self.headerView.showBlock = ^{
        [weakSelf createBgView:NO];
    };
    
    // 点击button
    self.headerView.buttonBlock = ^(NSMutableArray *items) {
        [weakSelf hideBgView];
    };
    
    ByViewPhysicalBuySegment *segment = [[ByViewPhysicalBuySegment alloc] init];
    [mainView addSubview:segment];
    [segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
    }];
    segment.selectBlock = ^(NSInteger selectIndex) {
        weakSelf.select_index = selectIndex;
        [self fetchDetailData];
    };
    
    self.contentView = [[UIView alloc] init];
    [mainView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(segment.mas_bottom);
        make.right.equalTo(@(0));
        make.bottom.equalTo(@(0));
    }];
    
}

// 创建固定套餐视图
- (void)createFixedViews{
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
    self.fixedTable = [[UITableView alloc] init];
    [self.contentView addSubview:self.fixedTable];
    [self.fixedTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.top.equalTo(@(0));
        make.bottom.equalTo(@(0));
    }];
    self.fixedTable.delegate = self;
    self.fixedTable.dataSource = self;
    
}

// 创建自选体检套餐视图
- (void)createOptionalViews{
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
    [self createOptionalBottomView];
    CGFloat width = [@"神经科科科科" getSizeOfString:14].width + 40;
    _classScroll = [[ByViewOptionalClassScroll alloc] init];
    [self.contentView addSubview:_classScroll];
    [_classScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.equalTo(@(0));
        make.width.equalTo(@(width));
        make.bottom.equalTo(@(-50));
    }];
    self.classScroll.classModels = [self.optionalAry copy];
    [self.classScroll reloadData];
    ByModelPhysicalOptional *first_model = self.optionalAry[0];
    [self getProductList:first_model];
    self.classScroll.selectingIndex = 0;
    [self setScrollClickBlock];
    
}
// 列表
- (void)getProductList:(ByModelPhysicalOptional *)model{
    NSMutableArray *temp = [NSMutableArray array];
    for (ByModelPhysicalOptional *sub_model in model.children) {
        [temp addObject:sub_model];
    }
    self.subClassScroll = [[ByViewOptionalSubClassScroll alloc]
                           init];
    [self.contentView addSubview:self.subClassScroll];
    [self.subClassScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_classScroll.mas_right);
        make.top.equalTo(@(0));
        make.right.equalTo(@(0));
        make.bottom.equalTo(@(-50));
    }];
    
    if (temp.count != 0) {
        self.subClassScroll.hidden = NO;
        self.subClassScroll.itemModels = [temp copy];
        [self.subClassScroll reloadData];
    }else{
        self.subClassScroll.hidden = YES;
    }
    
}
#pragma mark - 设置点击回调
- (void)setScrollClickBlock {
    __weak typeof(self) weakself = self;
    [self.classScroll setSelectBlock:^(ByModelPhysicalOptional *model) {
        [weakself getProductList:model];
    }];
}

// 创建确定购买
- (void)createOptionalBottomView{
    UIView *bottomView = [[UIView alloc] init];
    [self.contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.bottom.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    bottomView.backgroundColor = BY_THEME_COLOR;
    
    UIImageView *buyImg = [[UIImageView alloc] init];
    [bottomView addSubview:buyImg];
    [buyImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    buyImg.backgroundColor = [UIColor whiteColor];
    
    UILabel *countLabel = [[UILabel alloc] init];
    [bottomView addSubview:countLabel];
    CGFloat width = [@"1" getSizeOfString:12].width + 10;
    width = MIN(width, 20);
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(buyImg.mas_right);
        make.top.equalTo(buyImg.mas_top).offset(-(width - 5));
        make.size.mas_equalTo(CGSizeMake(width, width));
    }];
    countLabel.adjustsFontSizeToFitWidth = YES;
    countLabel.font = FONT(12);
    countLabel.layer.cornerRadius = width / 2;
    countLabel.textColor = [UIColor whiteColor];
    countLabel.backgroundColor = [UIColor redColor];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomView addSubview:sureButton];
    width = [@"确定购买" getSizeOfString:14].width + 10;
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@((SCREEN_WIDTH - width) / 2));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    [sureButton setTitle:@"确定购买" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureButton.titleLabel.font = FONT(14);
    
    UILabel *priceLabel = [[UILabel alloc] init];
    [bottomView addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buyImg.mas_right).offset(5);
        make.top.equalTo(@(15));
        make.right.equalTo(sureButton.mas_left);
        make.bottom.equalTo(@(-15));
    }];
    priceLabel.font = FONT(14);
    priceLabel.textColor = [UIColor whiteColor];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.fixedAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellPhysicalFixed *cell = [ByCellPhysicalFixed cellWithTableView:tableView];
    cell.model = self.fixedAry[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

#pragma mark - 隐藏视图
- (void)createBgView:(BOOL)isHiden{
    [self.bgView removeFromSuperview];
    [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, [self getHeightHeader]));
    }];
    self.bgView = [[UIView alloc] init];
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.top.equalTo(self.headerView.mas_bottom);
        make.bottom.equalTo(@(0));
    }];
    self.bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    self.bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView)];
    [self.bgView addGestureRecognizer:tap];
    self.bgView.hidden = isHiden;
}

- (void)hideBgView{
    self.bgView.hidden = YES;
    [self.bgView removeFromSuperview];
    [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    self.headerView.items = self.cates;
}
#pragma mark - 计算高度
- (CGFloat)getHeightHeader{
    CGFloat width,margin = 10;
    CGFloat x_offset = 10;
    CGFloat y_offset = 10;
    for (int i = 0; i < self.cates.count ; i ++) {
        ByModelPhysicalBuyCate *model = self.cates[i];
        width = [model.name getSizeOfString:14].width + 30;
        x_offset += width + margin;
        if (x_offset + width > SCREEN_WIDTH - 10) {
            x_offset = 10;
            y_offset += 30 + margin;
        }
    }
    
    return y_offset  + 30 + margin;
}
@end
